<?php

class Admin_Controller extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $this->load->library('ion_auth');
        $this->load->library('acl');
        $this->data['theme_path'] = $this->config->item('admin_theme_path');
        $controller = $this->router->fetch_class();
        $action = $this->router->fetch_method();
        if($controller != 'auth'){
            if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()) {
                if($controller != 'admincp'){
                    $redirect = admincp_url().'login?redirect_url='.current_url();
                }else{
                    $redirect = admincp_url().'login';
                }
		    	redirect($redirect, 'refresh');
		    }else{
                if (!$this->acl->has_permission($controller.'-'.$action)){
                    die('You don\'t access');
                }
            }
        }
        $this->db->cache_on();
        $this->render_lang();
        $this->render_translate();
        $this->breadcrumbs->push('Dashboard', 'admincp/dashboard/view');
        $this->db->cache_off();
    }

    public function _remap($method, $params = array()){
       if (method_exists($this, $method))
           {
            return call_user_func_array(array($this, $method), $params);
        }
        
        $this->show404();
    }

    function show404(){
        $this->output->set_status_header('404');
        $data['title'] = 'Page Not Found';
        $data['code'] = '404';
        $data['sub_title'] = 'The page you are looking for has not been found!';
        $data['sub_title_2'] = "The page you are looking for might have been removed, had its name changed, or unavailable. <br />Maybe you could try a search:";
        $this->parser->parse("error/error.tpl", NULL, $data);
    }

    public function render_lang($cache_delete = FALSE, $registry_delete = FALSE){
        if($cache_delete){
            $this->db->cache_delete_all();
        }
        if($registry_delete){
            $this->registry->delete('language');
        }
        $this->db->cache_on();
        $dataLang = $this->db->get('tpt_language')->result_array();
        if(!empty($dataLang)){
            if(!$this->registry->has('language')){
                $this->registry->set('language', $dataLang);
            }
        }
        $this->db->cache_off();
    }

    public function render_translate($cache_delete = FALSE, $registry_delete = FALSE){
        if($cache_delete){
            $this->db->cache_delete_all();
        }
        if($registry_delete){
            $this->registry->delete('translate');
        }
        $this->db->cache_on();
        $dataTranslate = $this->db->select('word,lang_code,translate')->get('tpt_translate')->result_array();
        if(!empty($dataTranslate)){
            if(!$this->registry->has('translate')){
                $translates = array();
                foreach($dataTranslate as $item){
                    $translates[$item['lang_code']][$item['word']] = $item['translate'];
                }
                $this->registry->set('translate', $translates);
            }
        }
        $this->db->cache_off();
    }

}